package com.dl7.mvp.module.video.main;

import android.util.Log;

import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.local.table.AlbumsListBean;
import com.dl7.mvp.local.table.VideoInfoDao;
import com.dl7.mvp.module.base.IRxBusPresenter;
import com.dl7.mvp.rxbus.RxBus;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by long on 2016/10/11.
 * Video 主界面 Presenter
 */
public class VideoMainPresenter implements IRxBusPresenter {

    private final VideoMainFragment mView;
    private final VideoInfoDao mDbDao;
    private final RxBus mRxBus;

    public VideoMainPresenter(VideoMainFragment view, VideoInfoDao dbDao, RxBus rxBus) {
        mView = view;
        mDbDao = dbDao;
        mRxBus = rxBus;
    }

    @Override
    public void getData() {
        Subscriber subscriber = new Subscriber<List<AlbumsListBean.DataBean>>() {
            @Override
            public void onCompleted() {
                Log.e("12345", "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                Log.e("12345", "onError");
            }

            @Override
            public void onNext(List<AlbumsListBean.DataBean> categoryList) {
                Log.e("12345", "categoryList: " + categoryList.size());
                mView.loadData(categoryList);
            }
        };
        RetrofitService.getCategoriesVideoList(0, 100, 20)
                .subscribe(subscriber);
    }

    @Override
    public void getMoreData() {
    }

    @Override
    public <T> void registerRxBus(Class<T> eventType, Action1<T> action) {
        Subscription subscription = mRxBus.doSubscribe(eventType, action, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Logger.e(throwable.toString());
            }
        });
        mRxBus.addSubscription(this, subscription);
    }

    @Override
    public void unregisterRxBus() {
        mRxBus.unSubscribe(this);
    }
}
